const assert = require('assert');
const BasicLayout = require('../BasicController');

module.exports = class Menu extends BasicLayout {
  constructor(ctx) {
    super(ctx);
    assert(think.isFunction(this.model), 'this.model must be a function');
    this.MODEL = this.ctx.state.ApiInfo.model;
    this.params = {
      model: this.MODEL,
      data: this.post()
    };
  }
  async delmenuAction() {
    const params = this.params;
    const datacheck = this.model(params.model).where({parent_id: params.data.id}).select();
    if (!think.isEmpty(datacheck)) {
      this.fail(1, '子菜单不为空无法删除', datacheck);
    } else {
      const data = await this.model(params.model).delAction(params);
      this.success(data, 'ok');
    }
  }
};

/**
 * @api {POST} /menu/menu/listall /listall
 * @apiDescription 获取菜单列表
 * @apiGroup menu
 *
 *
 * @apiVersion 1.0.0
 * @apiErrorExample {json} 错误返回值:
 *     {
 *        "code": ,
 *        "msg": "获取失败",
 *        "error": {
 *        },
 *     }
 *
 * @apiSuccessExample {json} 正确返回值:
 *     {
 *  "errno": 0,
 *  "errmsg": "",
 *   "data": [
 *       {
 *           "id": 8,
 *          "name": "菜单管理",
  *          "create_time": "2018-07-26 10:02:32",
  *          "update_time": "2018-07-26 13:35:01",
  *          "parent_id": 0,
  *          "url": null,
  *          "icon": null,
  *          "menu_level": "1"
*      }
 *   ],
 *   }
 */

/**
 * @api {post} /menu/menu/add /add
 * @apiDescription 新增菜单
 * @apiGroup menu
 *
 * @apiParam {String} name 菜单名字
 * @apiParam {String} parent_id 父级菜单id
 * @apiParam {String} menu_level 菜单级别
 * @apiParam {String} [url] 菜单跳转路由
 * @apiParam {String} [icon] 菜单图标
 *
 *
 *
 * @apiParamExample {Object} 请求参数格式:
 * {
 *   name: 用户列表,
 *   parent_id: 0，
 *   menu_level： 1
 * }
 *
 * @apiVersion 1.0.0
 * @apiErrorExample {json} 错误返回值:
 *     {
 *        "code": 500,
 *        "msg": "",
 *        "error": {
 *        },
 *     }
 * @apiSuccessExample {json} 正确返回值:
 *     {
 *  "errno": 0,
 *  "errmsg": "",
 *  "data": 11
 * }
 */

/**
 * @api {get} /menu/menu/get /get
 * @apiDescription 根据id查询单个菜单信息
 * @apiGroup menu
 *
 * @apiParam {Number} id 菜单id
 *
 *
 * @apiParamExample {string} 请求参数格式:
 *    ?id=1
 *
 * @apiVersion 1.0.0
 * @apiErrorExample {json} 错误返回值:
 *     {
 *        "code": 500,
 *        "msg": "",
 *        "error": {
 *        },
 *     }
 * @apiSuccessExample {json} 正确返回值:
*{
*    "errno": 0,
*    "errmsg": "",
*    "data": {
 *           "id": 8,
 *          "name": "菜单管理",
  *          "create_time": "2018-07-26 10:02:32",
  *          "update_time": "2018-07-26 13:35:01",
  *          "parent_id": 0,
  *          "url": null,
  *          "icon": null,
  *          "menu_level": "1"
*      }
*}
 */

/**
 * @api {delete} /menu/menu/delmenu /delmenu
 * @apiDescription 根据id删除菜单
 * @apiGroup menu
 *
 * @apiParam {String} id 菜单id
 *
 *
 * @apiParamExample {object} 请求参数格式:
 *  {
 * id: 2,
 * }
 *
 * @apiVersion 1.0.0
 * @apiErrorExample {json} 错误返回值:
 *     {
 *        "code": 1,
 *        "msg": "",
 *        "data": {
 *        },
 *     }
 * @apiSuccessExample {json} 正确返回值:
 *{
 *  "errno": 0,
 *   "errmsg": "",
 *   "data": 1
 *}
*}
 */

/**
 * @api {post} /menu/menu/edit /edit
 * @apiDescription 根据id查询修改菜单信息
 * @apiGroup menu
 *
 * @apiParam {String} id 菜单id
 * @apiParam {String} [url] 菜单跳转路由
 * @apiParam {String} [icon] 菜单logo
 * @apiParam {String} [menu_level] 菜单级别
 * @apiParam {String} [name] 菜单名字
 *
 *
 * @apiParamExample {object} 请求参数格式:
 *  {
 *  menu_level: '2',
 *  id: 6,
 *  name: '菜单测试'
 * }
 *
 * @apiVersion 1.0.0
 * @apiErrorExample {json} 错误返回值:
 *     {
 *        "code": 500,
 *        "msg": "",
 *        "error": {
 *        },
 *     }
 * @apiSuccessExample {json} 正确返回值:
*{
 *    "errno": 0,
 *   "errmsg": "",
 *   "data": 1
*}
*/
